package com.hxh.learn.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxh.common.unification.ResultOutcome;
import com.hxh.learn.model.SysRule;
import com.hxh.learn.model.SysUser;
import com.hxh.learn.service.ISysRuleService;
import com.hxh.learn.vo.SysRuleInsertVo;
import com.hxh.learn.vo.SysRuleSearchVo;
import com.hxh.learn.vo.SysRuleUpdateVo;
import com.hxh.learn.vo.SysUserSearchVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 系统角色 前端控制器
 * </p>
 *
 * @author hxh
 * @since 2023-03-02
 */
@RestController
@RequestMapping("/learn/sysRule")
public class SysRuleController {
    private ISysRuleService ruleService;

    @PostMapping("/add")
    @ApiOperation("添加角色")
    public ResultOutcome<String> add(@Validated @RequestBody SysRuleInsertVo ruleInsertVo){
        SysRule rule = ruleInsertVo.convert();
        boolean save = ruleService.save(rule);
        if (!save){
            return ResultOutcome.fileResult("添加角色失败");
        }
        return ResultOutcome.successResult();
    }

    @PostMapping("/update")
    @ApiOperation("修改角色")
    public ResultOutcome<String> update(@Validated @RequestBody SysRuleUpdateVo ruleUpdateVo){
        SysRule rule = ruleUpdateVo.convert();
        boolean save = ruleService.save(rule);
        if (!save){
            return ResultOutcome.fileResult("修改角色失败");
        }
        return ResultOutcome.successResult();
    }

    @GetMapping("/detail")
    @ApiOperation("详情")
    public ResultOutcome<SysRule> detail(String id) {
        SysRule rule = ruleService.getById(id);
        return ResultOutcome.successResult(rule);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户")
    public ResultOutcome<Boolean> delete(String id) {
        boolean remove = ruleService.removeById(id);
        if (!remove){
            ResultOutcome.fileResult("删除失败");
        }
        return ResultOutcome.successResult();
    }

    @PostMapping("/list")
    @ApiOperation("分页查询")
    public ResultOutcome<Page<SysRule>> list(@RequestBody SysRuleSearchVo sysRuleSearchVo) {
        Page<SysRule> sysUserPage = ruleService.queryRuleList(sysRuleSearchVo);
        return ResultOutcome.successResult(sysUserPage);
    }


}
